package projectsupporter.common.control;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.CLabel;

import projectsupporter.common.logic.PlugUILogic.ImagePath;
import org.eclipse.swt.graphics.Font;

public class BannerComposite extends Composite {

	private Label label = null;
	private Composite titleComposite = null;
	private CLabel cLabel = null;
	
	private Image bannerImage;
	private CLabel titleLabel = null;
	private CLabel infoLabel = null;
	public BannerComposite(Composite parent, int style,Image image) {
		super(parent, style);
		bannerImage=image;
		initialize();
	}

	private void initialize() {
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		gridData5.heightHint = 66;
		gridData5.verticalAlignment = org.eclipse.swt.layout.GridData.BEGINNING;
		gridData5.widthHint = 75;
		gridData5.grabExcessVerticalSpace = true;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		gridData.grabExcessHorizontalSpace = true;
		gridData.heightHint = 1;
		gridData.horizontalSpan = 2;
		gridData.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		createTitleComposite();
		cLabel = new CLabel(this, SWT.NONE);
		cLabel.setText("");
		cLabel.setLayoutData(gridData5);
		cLabel.addPaintListener(new org.eclipse.swt.events.PaintListener() {
			public void paintControl(org.eclipse.swt.events.PaintEvent e) {
				cLabelImagePaintControl(e);
			}
		});
		
		label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setText("Label");
		label.setLayoutData(gridData);
		GridLayout gridLayout3 = new GridLayout();
		gridLayout3.horizontalSpacing = 0;
		gridLayout3.marginWidth = 0;
		gridLayout3.verticalSpacing = 0;
		gridLayout3.numColumns = 7;
		gridLayout3.marginHeight = 0;
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.marginWidth = 0;
		gridLayout2.horizontalSpacing = 0;
		gridLayout2.marginHeight = 0;

		GridLayout gridLayout = new GridLayout();
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.numColumns = 3;
		gridLayout.marginHeight = 0;
		this.setLayout(gridLayout);
		this.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		this.setSize(new Point(525, 67));
	}


	protected void cLabelImagePaintControl(PaintEvent e) {
		if(bannerImage==null)
		{
			return;
		}
		
		Rectangle bounds = bannerImage.getBounds();
		Point size = cLabel.getSize();
		int MAX_WIDTH = size.x;//  缩放后的图片最大宽度
		int MAX_HEIGHT = size.y;//  缩放后的图片最大高度
		int imageWidth = bounds.width;
		int imageHeight = bounds.height;

		// determine thumbnail size from MAX_WIDTH and MAX_HEIGHT
		int thumbWidth = MAX_WIDTH;
		int thumbHeight = MAX_HEIGHT;
		double thumbRatio = (double) thumbWidth / (double) thumbHeight;
		double imageRatio = (double) imageWidth / (double) imageHeight;
		if (thumbRatio < imageRatio) {
		thumbHeight = (int) (thumbWidth / imageRatio);
		} else {
		thumbWidth = (int) (thumbHeight * imageRatio);
		}
		// 如果图片小于所略图大小, 不作处理
		if (imageWidth < MAX_WIDTH && imageHeight < MAX_HEIGHT) {
		thumbWidth = imageWidth;
		thumbHeight = imageHeight;
		}
		e.gc.drawImage(bannerImage, 0, 0, bounds.width, bounds.height, 0, 0, thumbWidth, thumbHeight);
		// evt.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight)
		
	}

	/**
	 * This method initializes titleComposite	
	 *
	 */
	private void createTitleComposite() {
		GridData gridData3 = new GridData();
		gridData3.grabExcessHorizontalSpace = true;
		gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.horizontalIndent = 24;
		gridData3.grabExcessVerticalSpace = true;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.verticalSpacing = 0;
		GridData gridData2 = new GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData1 = new GridData();
		gridData1.widthHint = -1;
		gridData1.heightHint = 66;
		gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.BEGINNING;
		titleComposite = new Composite(this, SWT.NONE);
		titleComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		titleComposite.setLayout(gridLayout1);
		titleComposite.setLayoutData(gridData1);
		titleLabel = new CLabel(titleComposite, SWT.NONE);
		titleLabel.setText("Title");
		titleLabel.setFont(new Font(Display.getDefault(), "メイリオ", 12, SWT.BOLD));
		titleLabel.setLayoutData(gridData2);
		titleLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		infoLabel = new CLabel(titleComposite, SWT.NONE);
		infoLabel.setText("CLabel");
		infoLabel.setLayoutData(gridData3);
		infoLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
	}
	
	public void setBanTitle(String title)
	{
		titleLabel.setText(title);
	}
	
	public void setBanInfo(String info)
	{
		infoLabel.setText(info);
	}

}  //  @jve:decl-index=0:visual-constraint="7,28"

